﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

/// <summary>
/// AdminDao 的摘要说明
/// </summary>
public class AdminDao
{
	public AdminDao()
	{
	}
    //获取当前用户的角色id
    public static int getRoleId(string adminId)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null && adminId.Trim().Length>0)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = "select roleid from admin where id="+adminId;
            try { 
                OleDbDataReader reader = comm.ExecuteReader();
                if (reader != null && reader.HasRows) {
                    reader.Read();
                    return reader.GetInt32(0);
                }
            }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
        return 0;
    }
    //添加
    public static void add(string roleDrop,string  userName,string  userPass,string  assDrop)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null && roleDrop.Trim().Length>0 && userName.Trim().Length>0 && userPass.Trim().Length>0 && assDrop.Trim().Length>0)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = "insert into admin (roleid, admin, [password], isAss) values(" + roleDrop + ",'" + userName + "','" + MD5.String2MD5(userPass) + "'," + assDrop + ")";
            try { comm.ExecuteNonQuery(); }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
    }
    //更新
    public static void update(string id, string roleDrop, string userName, string userPass, string assDrop)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null && roleDrop.Trim().Length > 0 && userName.Trim().Length > 0 && userPass.Trim().Length > 0 && assDrop.Trim().Length > 0)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = "update admin set roleid=" + roleDrop + ", admin='" + userName + "',[password]='" + userPass + "',isAss=" + assDrop + " where id=" + id;
            try { comm.ExecuteNonQuery(); }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
    }

    //删除
    public static void del(string id)
    {
        OleDbConnection conn = DB.conn();
        if (conn != null)
        {
            conn.Open();
            OleDbCommand comm = new OleDbCommand();
            try
            {
                comm.Connection = conn;
                //删除
                comm.CommandText = "delete from admin where id in (" + id + ")";
                comm.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { comm.Dispose(); conn.Close(); }
        }
    }
}
